package com.hcrazy.manchester.app.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hcrazy.manchester.app.form.RentSearchForm;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.Rent;
import com.hcrazy.manchester.app.services.RentService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/func")
public class RentController {
	
	@Autowired
	private RentService rentService;
	
	/**
	 * 跳转新增页面
	 * 初始化页面数据
	 * @param model
	 */
	@RequestMapping(value="/addRent",method=RequestMethod.GET)
	public void addRent(Model model){
		//初始化新增对象
		Rent rent = new Rent();
		rent.setRentType(1);
		rent.setHouseType(1);
		rent.setStatus(2);
		model.addAttribute("rent", rent);
	}
	
	/**
	 * 跳转新增页面
	 * 初始化页面数据
	 * @param model
	 */
	@RequestMapping(value="/addForRent",method=RequestMethod.GET)
	public void addForRent(Model model){
		//初始化新增对象
		Rent rent = new Rent();
		rent.setRentType(1);
		rent.setHouseType(1);
		rent.setStatus(2);
		model.addAttribute("rent", rent);
	}
	
	/**
	 * 查看信息内容
	 * @param rentId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/viewForRent",method=RequestMethod.GET)
	public void viewForRent(@RequestParam int rentId,Model model){
		try {
			//根据id获取信息内容
			Rent rent = rentService.getInfoById(rentId);
			//更新返回对象
			model.addAttribute("rent",rent);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
	}
	
	/**
	 * 编辑信息内容
	 * @param rentId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/editRent",method=RequestMethod.GET)
	public String editRent(@RequestParam int rentId,Model model){
		try {
			//初始化对象内容
			addRent(model);
			//根据id获取信息内容
			Rent rent = rentService.getInfoById(rentId);
			//更新返回对象
			model.addAttribute("rent",rent);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "/admin/func/addRent";
	}
	
	/**
	 * 编辑信息内容
	 * @param rentId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/editForRent",method=RequestMethod.GET)
	public String editForRent(@RequestParam int rentId,Model model){
		try {
			//初始化对象内容
			addRent(model);
			//根据id获取信息内容
			Rent rent = rentService.getInfoById(rentId);
			//更新返回对象
			model.addAttribute("rent",rent);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "/admin/func/addForRent";
	}
	
	/**
	 * 提交信息内容
	 * @param rent
	 * @return
	 */
	@RequestMapping(value="/rent",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public String addOrUpdateSort(@ModelAttribute("form") Rent rent,Model model){
		try {
			rentService.addOrUpdateRent(rent);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/func/rent";
    }
	
	/**
	 * 提交信息内容
	 * @param rent
	 * @return
	 */
	@RequestMapping(value="/forRent",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    public String addOrUpdateForRent(@ModelAttribute("form") Rent rent,Model model){
		try {
			rent.setTheType(Constant.RENT_TYPE_FOR_RENT);
			rentService.addOrUpdateRent(rent);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/func/forRent";
    }
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/rent",method=RequestMethod.GET)
	public void live_rent(Model model){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllRents(1,new RentSearchForm());
		model.addAttribute("pageModel", pageModel);
		
		//初始化查询对象
		model.addAttribute("searchForm", new RentSearchForm());
	}
	
	/**
	 * 跳转至求租功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/forRent",method=RequestMethod.GET)
	public void forRent(Model model){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		RentSearchForm form = new RentSearchForm();
		form.setTheType(Constant.RENT_TYPE_FOR_RENT);
		PageModel pageModel = getAllRents(1,form);
		model.addAttribute("pageModel", pageModel);
		
		//初始化查询对象
		model.addAttribute("searchForm", form);
	}
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_rent",method=RequestMethod.POST)
	public String searchRent(@ModelAttribute("form") RentSearchForm form,Model model){
		//是否执行删除操作
		if(form.getRentId() > 0){
			try {
				rentService.delRent(form.getRentId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置rentId
		form.setRentId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllRents(form.getPageNum(),form);
		model.addAttribute("pageModel", pageModel);
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/func/rent";
	}
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_for_rent",method=RequestMethod.POST)
	public String searchForRent(@ModelAttribute("form") RentSearchForm form,Model model){
		//是否执行删除操作
		if(form.getRentId() > 0){
			try {
				rentService.delRent(form.getRentId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置rentId
		form.setRentId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllRents(form.getPageNum(),form);
		model.addAttribute("pageModel", pageModel);
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/func/forRent";
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllRents(int pageNum,RentSearchForm form){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		try {
			return rentService.getRents(model,form.getTheType(),form.getRentType(), form.getHouseType(), form.getZipCode(),
					form.getAddress(), form.getStartArea(),form.getEndArea(), form.getStartPrice(),form.getEndPrice(),
					form.getTitle(), form.getLinkName(), form.getPhone(),form.getStartPublishDate(),form.getEndPublishDate(),form.getStatus());
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}

}
